<?php

namespace App\Http\Controllers\Admin;

use App\Models\SatisfactionSurvey;
use App\Models\SatisfactionSurveyReply;
use App\Validate\SatisfactionSurveyValidate;
use Illuminate\Support\Facades\DB;

/**
 * 满意度调查回复类
 * Class Survey
 * @package app\api\controller
 */
class SatisfactionSurveyReplyController extends CommonController
{


    // 自动验证
    public $validate = null;
    public $model = null;

    /**
     * 实例化问卷调查验证类
     * Survey constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->validate = new SatisfactionSurveyValidate();
        $this->model = new SatisfactionSurveyReply();
    }

    /**
     * 问卷调查用户回复列表
     * @param page 默认为 1
     * @param limit 限制条数 默认为 10
     * @param survey_id 问卷调查id
     * @param keywords 检索内容
     * @param is_satisfaction int 是否满意  1 满意  2 不满意
     * @param status int 是否核销  1 以核销  2 未核销（时间已过）3 待核销
     * @param start_time datetime 活动开始时间    数据格式  年月日
     * @param end_time datetime 活动结束时间
     * @param qr_code datetime 核销码
     */
    public function lists()
    {

        //增加验证场景进行验证
        if (!$this->validate->scene('reply_list')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $page = intval($this->request->page) ?: 1;
        $limit = $this->request->limit ?: 10;
        $survey_id = $this->request->survey_id;
        $keywords = $this->request->keywords;
        $is_satisfaction = $this->request->is_satisfaction;
        $status = $this->request->status;
        $start_time = $this->request->start_time;
        $end_time = $this->request->end_time;
        $qr_code = $this->request->qr_code;

        // 获取数据
        $res = $this->model->lists($survey_id, null, $keywords, $is_satisfaction, $status, $start_time, $end_time, $qr_code, $limit);

        if (empty($res['data'])) {
            return $this->returnApi(203, "暂无数据");
        }
        $res = $this->disPageData($res);
        $res['data'] = $this->addSerialNumber($res['data']);

        //计算满意度
        $satisfaction = $this->model->getUserSubmitCount($survey_id, null, null, 1); //满意数量
        $not_satisfaction = $this->model->getUserSubmitCount($survey_id, null, null, 2); //不满意数量
        $res['satisfaction'] = $satisfaction;
        $res['not_satisfaction'] = $not_satisfaction;

        $total_number = $satisfaction + $not_satisfaction; //
        $res['satisfaction_percent'] = $total_number ? sprintf("%.2f", $satisfaction / $total_number) : '0.00';
        $res['not_satisfaction_percent'] = $total_number ? sprintf("%.2f", (1 - $res['satisfaction_percent'])) : '0.00';

        return $this->returnApi(200, "查询成功", true, $res);
    }


    /**
     * 核销码进行核销
     * @param survey_id 问卷调查id
     * @param qr_code 核销码
     */
    public function qrCodeVerification()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('qr_code_verification')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        // 获取问卷调查是否存在
        $res = SatisfactionSurvey::select('id', 'start_time', 'end_time')->where("is_del", 1)->where('id', $this->request->survey_id)->first();
        if (empty($res)) {
            return $this->returnApi(203, "数据不存在");
        }
        if ($res['start_time'] > date('Y-m-d H:i:s') || $res['end_time'] < date('Y-m-d H:i:s')) {
            return $this->returnApi(203, "当前时间不在范围内");
        }

        DB::beginTransaction();
        try {
            $this->model->verification(null, $this->request->qr_code);

            DB::commit();
            return $this->returnApi(200, "核销成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage() ? $e->getMessage() : "核销失败");
        }
    }

    /**
     * 手动进行核销
     * @param survey_id 问卷调查id
     * @param id 数据id
     */
    public function handVerification()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('qr_code_verification')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }
        // 获取问卷调查是否存在
        $res = SatisfactionSurvey::select('id', 'start_time', 'end_time')->where("is_del", 1)->where('id', $this->request->survey_id)->first();
        if (empty($res)) {
            return $this->returnApi(203, "数据不存在");
        }
        if ($res['start_time'] > date('Y-m-d H:i:s') || $res['end_time'] < date('Y-m-d H:i:s')) {
            return $this->returnApi(203, "当前时间不在范围内");
        }

        DB::beginTransaction();
        try {
            $this->model->verification($this->request->id);

            DB::commit();
            return $this->returnApi(200, "核销成功", true);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->returnApi(202, $e->getMessage() ? $e->getMessage() : "核销失败");
        }
    }
}
